let currentStep = 1;
const totalSteps = 2;
const step1 = document.getElementById('step1');
const step2 = document.getElementById('step2');
const stepIndicator = document.getElementById('stepIndicator');

// 切换步骤
function nextStep(step) {
    if (step === 1 && !validateStep1()) return;

    currentStep++;
    updateStepIndicator();
    step1.classList.add('hidden');
    step2.classList.remove('hidden');
}

function prevStep() {
    currentStep--;
    updateStepIndicator();
    step1.classList.remove('hidden');
    step2.classList.add('hidden');
}

// 步骤指示器更新
function updateStepIndicator() {
    const width = (currentStep / totalSteps) * 100;
    stepIndicator.style.setProperty('--width', `${width}%`);
}

// 第一步验证
function validateStep1() {
    const phone = document.getElementById('phone').value.trim();
    const email = document.getElementById('email').value.trim();
    const username = document.getElementById('username').value.trim();
    const password = document.getElementById('password').value.trim();
    const phoneError = document.getElementById('phoneError');
    const emailError = document.getElementById('emailError');
    const usernameError = document.getElementById('usernameError');
    const passwordError = document.getElementById('passwordError');
    let isValid = true;

    // 手机号验证
    if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
        phoneError.classList.remove('hidden');
        isValid = false;
    } else {
        phoneError.classList.add('hidden');
    }

    // 邮箱验证
    if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
        emailError.classList.remove('hidden');
        isValid = false;
    } else {
        emailError.classList.add('hidden');
    }

    // 用户名验证
    if (!username || username.length < 3 || username.length > 20) {
        usernameError.classList.remove('hidden');
        isValid = false;
    } else {
        usernameError.classList.add('hidden');
    }

    // 密码验证
    if (!password || password.length < 6) {
        passwordError.classList.remove('hidden');
        isValid = false;
    } else {
        passwordError.classList.add('hidden');
    }

    return isValid;
}

// 第二步验证
function validateStep2() {
    const jobInterest = document.getElementById('jobInterest').value.trim();
    const techTack = document.getElementById('techTack').value.trim();
    const jobInterestError = document.getElementById('jobInterestError');
    const techTackError = document.getElementById('techTackError');
    let isValid = true;

    if (!jobInterest) {
        jobInterestError.classList.remove('hidden');
        isValid = false;
    } else {
        jobInterestError.classList.add('hidden');
    }

    if (!techTack) {
        techTackError.classList.remove('hidden');
        isValid = false;
    } else {
        techTackError.classList.add('hidden');
    }

    return isValid;
}

// 提交注册
async function submitRegistration() {
    if (!validateStep2()) return;

    // 收集表单数据
    const formData = {
        phone: document.getElementById('phone').value,
        email: document.getElementById('email').value,
        username: document.getElementById('username').value,
        password: document.getElementById('password').value,
        nickName: document.getElementById('nickName').value || null,
        jobInterest: document.getElementById('jobInterest').value,
        techTack: document.getElementById('techTack').value
    };

    try {
        // 发送注册请求
        const response = await fetch('/user/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(formData),
            credentials: 'include' // 携带Cookie
        });

        if (response.ok) {
            const result = await response.json();
            if (result.code === 200) {
                await Swal.fire({
                    icon: 'success',
                    title: '注册成功！',
                    text: '即将跳转到题库页面',
                    timer: 1500,
                    showConfirmButton: false,
                    toast: true,
                    position: 'top-end',
                    background: '#f0f9eb',
                    iconColor: '#52c41a',
                    color: '#303133'
                });
                window.location.href = '/view/quesList.html';
            } else {
                throw new Error(result.message || '注册失败');
            }
        } else {
            throw new Error('服务器错误');
        }
    } catch (error) {
        await Swal.fire({
            icon: 'error',
            title: '注册失败',
            text: error.message || '请检查输入信息',
            toast: true,
            position: 'top-end',
            showConfirmButton: false,
            timer: 2000,
            background: '#fef0f0',
            iconColor: '#ff4d4f',
            color: '#606266'
        });
    }
}